<template>
  <div class="center">
    <div class="search clearfix">
      <Search @childFn="parentFn" />
      <el-select
        v-model="sitenum"
        :clearable="true"
        :filterable="true"
        placeholder="请选择站点"
        :style="{ width: '200px' }"
      >
        <el-option
          v-for="item in sitelist"
          :label="item.siteName"
          :key="item.index"
          :value="item.siteNo"
        >
        </el-option>
      </el-select>
      <el-button type="primary" @click="sub">查询</el-button>
    </div>
    <div class="line_box">
      <dv-border-box-11 :color="['#11caff']" backgroundColor="transparent" title="全站温度曲线">
      <div class="templine"></div>
      </dv-border-box-11>
    </div>
    <div class="main">
      <dv-border-box-11 :color="['#11caff']" backgroundColor="transparent" title="温度监控">
      <div class="timer clearfix" v-if="show">
        <span>时间范围</span>
        <div style="display: inline-block; float: left">
          <el-radio-group v-model="radio" fill="#00306F" @change="timeChange">
            <el-radio-button label="10分钟"></el-radio-button>
            <el-radio-button label="30分钟"></el-radio-button>
            <el-radio-button label="60分钟"></el-radio-button>
            <el-radio-button label="3小时"></el-radio-button>
            <el-radio-button label="6小时"></el-radio-button>
            <el-radio-button label="24小时"></el-radio-button>
          </el-radio-group>
        </div>
      </div>
      <div class="UPS"></div>
      <div class="charging_rack"></div>
      <div class="trough"></div>
      </dv-border-box-11>
    </div>
  </div>
</template>
<script>
import Search from "../../common/select";
import elementResizeDetectorMaker from "element-resize-detector";
export default {
  name: "curves",
  components: {
    Search,
  },
  data() {
    return {
      radio: "10分钟",
      timer: 10, //默认时间
      interval: "", //定时器
      show: false,
      message: {},
      sitelist: [],
      warehouse: {}, //电池仓阈值集合
      cabinet: {}, //充电柜阈值集合
      line: {}, //线槽阈值集合
      $chart1: "",
      $chart2: "",
      $chart3: "",
      $chart4: "",
      sitename: "",
      sitenum: "",
      option: {
        title: {
          text: "电池仓",
          left: "20",
          top: "20",
          textStyle: {
            color: "#fff",
          },
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          top: "20",
        },
        toolbox: {
          show: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          axisLine: {
            lineStyle: {
              color: "#133579",
              width: 2,
            },
          },
          data: [],
          axisLabel: {
            show: true,
            interval: "auto",
            textStyle: {
              color: "#fff", //更改坐标轴文字颜色
            },
          },
        },
        yAxis: {
          type: "value",
          min: 0,
          max: 80,
          interval: 10,
          axisLine: {
            lineStyle: {
              width: 0,
            },
          },
          axisLabel: {
            formatter: "{value} °C",
            textStyle: {
              color: "#fff", //更改坐标轴文字颜色
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "#11366e",
              type: "dashed",
            },
          },
          axisTick: {
            //y轴刻度线
            show: false,
          },
        },
        series: [
          {
            type: "line",
            smooth: true,
            color: "#00cdfa",
            data: [],
            markPoint: {
              data: [
                { type: "max", name: "最大值" },
                { type: "min", name: "最小值" },
              ],
            },
            markLine: {
              label: {
                position: "end", //将警示值放在哪个位置，三个值“start”,"middle","end"  开始  中点 结束
                formatter: "",
              },
              data: [
                {
                  lineStyle: {
                    //警戒线的样式  ，虚实  颜色
                    type: "solid",
                    color: "#FCB724",
                    width: 2,
                  },
                  yAxis: "", //警戒线在y轴的坐标
                },
              ],
            },
          },
          {
            type: "line",
            smooth: true,
            markLine: {
              label: {
                position: "end", //将警示值放在哪个位置，三个值“start”,"middle","end"  开始  中点 结束
                formatter: "",
              },
              data: [
                {
                  lineStyle: {
                    //警戒线的样式  ，虚实  颜色
                    type: "solid",
                    color: "#FC6924",
                    width: 2,
                  },
                  yAxis: "", //警戒线在y轴的坐标
                },
              ],
            },
          },
          {
            type: "line",
            smooth: true,
            markLine: {
              label: {
                position: "end", //将警示值放在哪个位置，三个值“start”,"middle","end"  开始  中点 结束
                formatter: "",
              },
              data: [
                {
                  lineStyle: {
                    //警戒线的样式  ，虚实  颜色
                    type: "solid",
                    color: "#FF0A1B",
                    width: 2,
                  },
                  yAxis: "", //警戒线在y轴的坐标
                },
              ],
            },
          },
        ],
      },
      option1: {
        title: {
          text: "充电柜",
          left: "20",
          top: "20",
          textStyle: {
            color: "#fff",
          },
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          top: "20",
        },
        toolbox: {
          show: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: [],
          axisLine: {
            lineStyle: {
              color: "#133579",
              width: 2,
            },
          },
          axisLabel: {
            show: true,
            interval: "auto",
            textStyle: {
              color: "#fff", //更改坐标轴文字颜色
            },
          },
        },
        yAxis: {
          type: "value",
          color: "#00cdfa",
          min: 20,
          max: 100,
          interval: 10,
          axisLine: {
            lineStyle: {
              width: 0,
            },
          },
          axisLabel: {
            formatter: "{value} °C",
            textStyle: {
              color: "#fff", //更改坐标轴文字颜色
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "#11366e",
              type: "dashed",
            },
          },
          axisTick: {
            //y轴刻度线
            show: false,
          },
        },
        series: [
          {
            type: "line",
            smooth: true,
            color: "#00cdfa",
            data: [],
            markPoint: {
              data: [
                { type: "max", name: "最大值" },
                { type: "min", name: "最小值" },
              ],
            },
            markLine: {
              label: {
                position: "end", //将警示值放在哪个位置，三个值“start”,"middle","end"  开始  中点 结束
                formatter: "",
              },
              data: [
                {
                  lineStyle: {
                    //警戒线的样式  ，虚实  颜色
                    type: "solid",
                    color: "#FCB724",
                    width: 2,
                  },
                  yAxis: "", //警戒线在y轴的坐标
                },
              ],
            },
          },
          {
            type: "line",
            smooth: true,
            markLine: {
              label: {
                position: "end", //将警示值放在哪个位置，三个值“start”,"middle","end"  开始  中点 结束
                formatter: "",
              },
              data: [
                {
                  lineStyle: {
                    //警戒线的样式  ，虚实  颜色
                    type: "solid",
                    color: "#FC6924",
                    width: 2,
                  },
                  yAxis: "", //警戒线在y轴的坐标
                },
              ],
            },
          },
          {
            type: "line",
            smooth: true,
            markLine: {
              label: {
                position: "end", //将警示值放在哪个位置，三个值“start”,"middle","end"  开始  中点 结束
                formatter: "",
              },
              data: [
                {
                  lineStyle: {
                    //警戒线的样式  ，虚实  颜色
                    type: "solid",
                    color: "#FF0A1B",
                    width: 2,
                  },
                  yAxis: "", //警戒线在y轴的坐标
                },
              ],
            },
          },
        ],
      },
      option2: {
        title: {
          text: "线槽",
          left: "20",
          top: "20",
          textStyle: {
            color: "#fff",
          },
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          top: "20",
        },
        toolbox: {
          show: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          axisLine: {
            lineStyle: {
              color: "#133579",
              width: 2,
            },
          },
          data: [],
          axisLabel: {
            show: true,
            interval: "auto",
            textStyle: {
              color: "#fff", //更改坐标轴文字颜色
            },
          },
        },
        yAxis: {
          type: "value",
          color: "#00cdfa",
          min: 0,
          max: 100,
          interval: 10,
          axisLine: {
            lineStyle: {
              width: 0,
            },
          },
          axisLabel: {
            formatter: "{value} °C",
            textStyle: {
              color: "#fff", //更改坐标轴文字颜色
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "#11366e",
              type: "dashed",
            },
          },
          axisTick: {
            //y轴刻度线
            show: false,
          },
        },
        series: [
          {
            type: "line",
            color: "#00cdfa",
            smooth: true,
            data: [],
            markPoint: {
              data: [
                { type: "max", name: "最大值" },
                { type: "min", name: "最小值" },
              ],
            },
            markLine: {
              label: {
                position: "end", //将警示值放在哪个位置，三个值“start”,"middle","end"  开始  中点 结束
                formatter: "",
              },
              data: [
                {
                  lineStyle: {
                    //警戒线的样式  ，虚实  颜色
                    type: "solid",
                    color: "#FCB724",
                    width: 2,
                  },
                  yAxis: "", //警戒线在y轴的坐标
                },
              ],
            },
          },
          {
            type: "line",
            smooth: true,
            markLine: {
              label: {
                position: "end", //将警示值放在哪个位置，三个值“start”,"middle","end"  开始  中点 结束
                formatter: "",
              },
              data: [
                {
                  lineStyle: {
                    //警戒线的样式  ，虚实  颜色
                    type: "solid",
                    color: "#FC6924",
                    width: 2,
                  },
                  yAxis: "", //警戒线在y轴的坐标
                },
              ],
            },
          },
          {
            type: "line",
            smooth: true,
            markLine: {
              label: {
                position: "end", //将警示值放在哪个位置，三个值“start”,"middle","end"  开始  中点 结束
                formatter: "",
              },
              data: [
                {
                  lineStyle: {
                    //警戒线的样式  ，虚实  颜色
                    type: "solid",
                    color: "#FF0A1B",
                    width: 2,
                  },
                  yAxis: "", //警戒线在y轴的坐标
                },
              ],
            },
          },
        ],
      },
      option3: {
        backgroundColor: "transparent",
        legend: {
          data: ["最高温", "最低温"],
          textStyle: {
            color: "#fff",
          },
          top: "10",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            lineStyle: {
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: "rgba(0, 255, 233,0)",
                  },
                  {
                    offset: 0.5,
                    color: "rgba(255, 255, 255,1)",
                  },
                  {
                    offset: 1,
                    color: "rgba(0, 255, 233,0)",
                  },
                ],
                global: false,
              },
            },
          },
        },
        grid: {
          x: 60,
          y: 50,
          x2: 60,
          y2: 60,
        },
        xAxis: [
          {
            type: "category",
            axisLine: {
              show: true,
              lineStyle: {
                color: "#133579",
                width: 2,
              },
            },
            splitArea: {
              color: "#f00",
              lineStyle: {
                color: "#f00",
              },
            },
            axisLabel: {
              color: "#fff",
              show: true,
              interval: "auto",
            },
            splitLine: {
              show: false,
            },
            boundaryGap: false,
            data: [],
          },
        ],
        yAxis: [
          {
            type: "value",
            min: 0,
            splitNumber: 4,
            splitLine: {
              show: true,
              lineStyle: {
                color: "#11366e",
                type: "dashed",
              },
            },
            axisLine: {
              show: false,
            },
            axisLabel: {
              show: true,
              margin: 20,
              textStyle: {
                color: "#d1e6eb",
              },
            },
            axisTick: {
              show: false,
            },
          },
        ],
        series: [
          {
            name: "最高温",
            type: "line",
            smooth: true, //是否平滑
            showAllSymbol: true,
            symbol: "circle",
            symbolSize: 8,
            lineStyle: {
              normal: {
                color: "rgba(252, 105, 36, 1)",
              },
            },
            label: {
              show: true,
              position: "top",
              textStyle: {
                color: "rgba(252, 105, 36, 1)",
              },
            },
            itemStyle: {
              color: "rgba(252, 105, 36, 1)",
              borderColor: "#fff",
            },
            tooltip: {
              show: false,
            },
            areaStyle: {
              normal: {
                color: new this.$echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: "rgba(0,179,244,0.3)",
                    },
                    {
                      offset: 1,
                      color: "rgba(0,179,244,0)",
                    },
                  ],
                  false
                ),
              },
            },
            data: [],
          },
          {
            name: "最低温",
            type: "line",
            smooth: true, //是否平滑
            showAllSymbol: true,
            symbol: "circle",
            symbolSize: 8,
            itemStyle: {
              color: "rgba(11, 211, 185, 1)",
              borderColor: "#fff",
            },
            tooltip: {
              show: true,
            },
            areaStyle: {
              normal: {
                color: new this.$echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: "rgba(0,202,149,0.3)",
                    },
                    {
                      offset: 1,
                      color: "rgba(0,202,149,0)",
                    },
                  ],
                  false
                ),
              },
            },
            data: [],
          },
        ],
      },
    };
  },
  methods: {
    //接收子组件传的省市区对象
    parentFn(payload) {
      this.message = payload;
      this.getsitelist();
    },
    //查询
    timeChange(val) {
      this.sub(val);
    },
    sub(time) {
      if (!this.show) {
        this.show = true;
      }
      if (time == "10分钟") {
        this.timer = 10;
      }
      if (time == "30分钟") {
        this.timer = 30;
      }
      if (time == "60分钟") {
        this.timer = 60;
      }
      if (time == "3小时") {
        this.timer = 60 * 3;
      }
      if (time == "6小时") {
        this.timer = 60 * 6;
      }
      if (time == "24小时") {
        this.timer = 60 * 24;
      }
      let params = {
        deviceNo: this.sitenum,
        duration: this.timer,
      };
      this.$http
        .post("/redis/getTempDate", params)
        .then((res) => {
          if (res.data.h.code == 0) {
            this.option.xAxis.data = res.data.b.time;
            this.option.series[0].data = res.data.b.warehouseAvg;
            this.option1.xAxis.data = res.data.b.time;
            this.option1.series[0].data = res.data.b.cabinetAvg;
            this.option2.xAxis.data = res.data.b.time;
            this.option2.series[0].data = res.data.b.lineAvg;
            this.option3.xAxis[0].data = res.data.b.time;
            this.option3.series[0].data = res.data.b.max;
            this.option3.series[1].data = res.data.b.min;
            this.initbar();
            this.$chart1.setOption({
              series: [
                {
                  markLine: {
                    label: {
                      formatter: this.warehouse.levelOne,
                    },
                    data: [
                      {
                        lineStyle: {
                          //警戒线的样式  ，虚实  颜色
                          type: "solid",
                          color: "#FCB724",
                          width: 2,
                        },
                        yAxis: this.warehouse.levelOne, //警戒线在y轴的坐标
                      },
                    ],
                  },
                },
                {
                  markLine: {
                    label: {
                      formatter: this.warehouse.levelTwo,
                    },
                    data: [
                      {
                        lineStyle: {
                          //警戒线的样式  ，虚实  颜色
                          type: "solid",
                          color: "#FC6924",
                          width: 2,
                        },
                        yAxis: this.warehouse.levelTwo, //警戒线在y轴的坐标
                      },
                    ],
                  },
                },
                {
                  markLine: {
                    label: {
                      formatter: this.warehouse.levelThree,
                    },
                    data: [
                      {
                        lineStyle: {
                          //警戒线的样式  ，虚实  颜色
                          type: "solid",
                          color: "#FF0A1B",
                          width: 2,
                        },
                        yAxis: this.warehouse.levelThree, //警戒线在y轴的坐标
                      },
                    ],
                  },
                },
              ],
            });
            this.$chart2.setOption({
              series: [
                {
                  markLine: {
                    label: {
                      formatter: this.cabinet.levelOne,
                    },
                    data: [
                      {
                        lineStyle: {
                          //警戒线的样式  ，虚实  颜色
                          type: "solid",
                          color: "#FCB724",
                          width: 2,
                        },
                        yAxis: this.cabinet.levelOne, //警戒线在y轴的坐标
                      },
                    ],
                  },
                },
                {
                  markLine: {
                    label: {
                      formatter: this.cabinet.levelTwo,
                    },
                    data: [
                      {
                        lineStyle: {
                          //警戒线的样式  ，虚实  颜色
                          type: "solid",
                          color: "#FC6924",
                          width: 2,
                        },
                        yAxis: this.cabinet.levelTwo, //警戒线在y轴的坐标
                      },
                    ],
                  },
                },
                {
                  markLine: {
                    label: {
                      formatter: this.cabinet.levelThree,
                    },
                    data: [
                      {
                        lineStyle: {
                          //警戒线的样式  ，虚实  颜色
                          type: "solid",
                          color: "#FF0A1B",
                          width: 2,
                        },
                        yAxis: this.cabinet.levelThree, //警戒线在y轴的坐标
                      },
                    ],
                  },
                },
              ],
            });
            this.$chart3.setOption({
              series: [
                {
                  markLine: {
                    label: {
                      formatter: this.line.levelOne,
                    },
                    data: [
                      {
                        lineStyle: {
                          //警戒线的样式  ，虚实  颜色
                          type: "solid",
                          color: "#FCB724",
                          width: 2,
                        },
                        yAxis: this.line.levelOne, //警戒线在y轴的坐标
                      },
                    ],
                  },
                },
                {
                  markLine: {
                    label: {
                      formatter: this.line.levelTwo,
                    },
                    data: [
                      {
                        lineStyle: {
                          //警戒线的样式  ，虚实  颜色
                          type: "solid",
                          color: "#FC6924",
                          width: 2,
                        },
                        yAxis: this.line.levelTwo, //警戒线在y轴的坐标
                      },
                    ],
                  },
                },
                {
                  markLine: {
                    label: {
                      formatter: this.line.levelThree,
                    },
                    data: [
                      {
                        lineStyle: {
                          //警戒线的样式  ，虚实  颜色
                          type: "solid",
                          color: "#FF0A1B",
                          width: 2,
                        },
                        yAxis: this.line.levelThree, //警戒线在y轴的坐标
                      },
                    ],
                  },
                },
              ],
            });
            if (this.timer > 10) {
              this.$chart1.setOption({
                dataZoom: [
                  {
                    type: "inside",
                    startValue: 55,
                    endValue:65,
                  },
                ],
              });
              this.$chart2.setOption({
                dataZoom: [
                  {
                    type: "inside",
                    startValue: 55,
                    endValue:65,
                  },
                ],
              });
              this.$chart3.setOption({
                dataZoom: [
                  {
                    type: "inside",
                    startValue: 55,
                    endValue:65,
                  },
                ],
              });
              this.$chart4.setOption({
                dataZoom: [
                  {
                    type: "inside",
                    startValue: 55,
                    endValue:65,
                  },
                ],
              });
            }
          }
        })
        .catch((err) => {});
    },
    //获取站点列表
    getsitelist() {
      let param = {
        provinceCode: this.message.provincecode,
        cityCode: this.message.citycode,
        districtCode: this.message.districtcode,
      };
      this.$http
        .post("/site/device/select", param)
        .then((res) => {
          if (res.data.h.code == 0) {
            this.sitelist = res.data.b;
          }
        })
        .catch((err) => {});
    },
    //获取告警阈值
    getalarmvalue() {
      this.$http
        .get("/redis/alarmValue")
        .then((res) => {
          if (res.data.h.code == 0) {
            this.warehouse = res.data.b.warehouse;
            this.cabinet = res.data.b.cabinet;
            this.line = res.data.b.line;
          }
        })
        .catch((err) => {});
    },
    //分页
    handleCurrentChange() {},
    //初始化折线图容器
    initbox() {
      this.$chart1 = this.$echarts.init(document.querySelector(".UPS"));
      this.$chart2 = this.$echarts.init(
        document.querySelector(".charging_rack")
      );
      this.$chart3 = this.$echarts.init(document.querySelector(".trough"));
      this.$chart4 = this.$echarts.init(document.querySelector(".templine"));
    },
    //折线图渲染
    initbar() {
      this.$chart1.setOption(this.option);
      this.$chart2.setOption(this.option1);
      this.$chart3.setOption(this.option2);
      this.$chart4.setOption(this.option3);
    },
  },
  created() {
    this.getalarmvalue();
  },
  mounted() {
    this.initbox();
    this.getsitelist();
    this.interval = setInterval(this.sub, 30000);
    var erd = elementResizeDetectorMaker();
    erd.listenTo(document.querySelector(".UPS"), function (element) {
      this.$nextTick(function () {
        //使echarts尺寸重置
        this.$echarts.init(document.querySelector(".UPS")).resize();
      });
    }.bind(this));
    erd.listenTo(document.querySelector(".charging_rack"), function (element) {
      this.$nextTick(function () {
        //使echarts尺寸重置
        this.$echarts.init(document.querySelector(".charging_rack")).resize();
      });
    }.bind(this));
    erd.listenTo(document.querySelector(".trough"), function (element) {
      this.$nextTick(function () {
        //使echarts尺寸重置
        this.$echarts.init(document.querySelector(".trough")).resize();
      });
    }.bind(this));
    erd.listenTo(document.querySelector(".templine"), function (element) {
      this.$nextTick(function () {
        //使echarts尺寸重置
        this.$echarts.init(document.querySelector(".templine")).resize();
      });
    }.bind(this));
  },
  beforeDestroy() {
    clearInterval(this.interval);
    this.interval=null;
  },
};
</script>
<style scoped>
.search {
  padding-left: 20px;
  box-sizing: border-box;
  display: inline-block;
}
.search .el-select {
  float: left;
  width: 120px;
  margin-right: 10px;
}
.search .el-input {
  float: left;
  width: 150px;
  margin-right: 10px;
}
.line_box {
  height: 338px;
}
.templine {
  height: 280px;
}
.main {
  width: 100%;
  height: 920px;
  position: relative;
}
.timer {
  height: 40px;
  line-height: 40px;
  position: absolute;
  clear: both;
  top: 20px;
  right: 20px;
  z-index: 1;
  border-radius: 5px;
  padding: 5px;
  color: rgba(184, 222, 255, 1);
}
.timer span {
  display: inline-block;
  float: left;
  margin-right: 10px;
}
.timer /deep/ .el-radio-button__inner {
  border: 0;
  color: rgba(184, 222, 255, 1);
  background: transparent;
  box-shadow: none;
}
.timer .el-radio-button__inner {
  border: 0;
  color: rgba(184, 222, 255, 1);
  background: transparent;
  box-shadow: none;
}
.UPS,
.charging_rack,
.trough {
  height: 30%;
  background: transparent;
  margin-bottom: 20px;
  width: 100%;
}
</style>
